<template>
  <el-dialog
    v-model="props.childrenDialog"
    :title="props.childrenTitle"
    :show-close="false"
    width="35%"
  >
    <el-form :model="form" :rules="rules" ref="ruleFormRef">
      <el-form-item label="所属故障类型">
        {{ props.childrenForm?.gzName }}
      </el-form-item>
      <el-form-item label="故障现象" prop="failureName">
        <el-input
          v-model="form.failureName"
          autocomplete="off"
          placeholder="请输入故障现象"
          type="textarea"
          row="4"
          show-word-limit
          maxlength="128"
        />
      </el-form-item>
      <el-form-item label="解决方案" prop="solution">
        <el-input
          v-model="form.solution"
          autocomplete="off"
          placeholder="请输入解决方案"
          type="textarea"
          row="4"
          show-word-limit
          maxlength="128"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel(ruleFormRef)">取消</el-button>
        <el-button
          type="primary"
          :loading="loading"
          @click="submit(ruleFormRef)"
        >
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { ref, reactive, getCurrentInstance, watch } from 'vue'
import { addFailureTermApi, editFailureTermApi } from '/@/api/repair'
const { proxy } = getCurrentInstance() as any
const props = defineProps({
  childrenDialog: Boolean,
  childrenForm: Object,
  childrenTitle: String
})
const emit = defineEmits(['childrenCancel', 'childrenSubmit'])
const ruleFormRef = ref<FormInstance>()
const formLabelWidth = ref('100px')
const loading = ref(false)
const form = ref({
  parentId: '',
  deviceType: '',
  deviceTypeName: '',
  failureName: '',
  solution: ''
})

const rules = reactive<FormRules>({
  failureName: [{ required: true, message: '请输入故障现象', trigger: 'blur' }],
  solution: [{ required: true, message: '请输入解决方案', trigger: 'blur' }]
})

const cancel = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  emit('childrenCancel')
}

const submit = (formEl: FormInstance | undefined) => {
  loading.value = true
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
      form.value.deviceType = props.childrenForm?.deviceType
      form.value.deviceTypeName = props.childrenForm?.deviceTypeName
      if (props.childrenTitle == '新增') {
        form.value.parentId = props.childrenForm?.id
        addFailureTermApi(form.value).then((res) => {
          if (res.code == 0) {
            ElMessage.success('新增成功')
            loading.value = false
            emit('childrenSubmit')
          } else {
            ElMessage.success('新增失败')
            loading.value = false
          }
        })
      } else {
        Reflect.set(form.value, 'id', props.childrenForm?.id)
        form.value.parentId = props.childrenForm?.parentId
        editFailureTermApi(form.value).then((res) => {
          if (res.code == 0) {
            ElMessage.success('编辑成功')
            loading.value = false
            emit('childrenSubmit')
          } else {
            ElMessage.success('编辑失败')
            loading.value = false
          }
        })
      }
    } else {
      loading.value = false
    }
  })
}

watch(
  () => props.childrenDialog,
  () => {
    if (props.childrenTitle == '编辑') {
      form.value.solution = props.childrenForm?.solution
      form.value.failureName = props.childrenForm?.failureName
    } else {
      form.value.solution = ''
      form.value.failureName = ''
    }
  }
)
</script>
<style scoped lang="scss"></style>
